package com.wl.shixun3.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 检查会话控制器（用于AJAX请求）
 */
@WebServlet("/shixun3/checkSession")
public class CheckSessionController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        
        PrintWriter out = response.getWriter();
        
        HttpSession session = request.getSession(false);
        
        if (session != null && 
            session.getAttribute("user") != null && 
            Boolean.TRUE.equals(session.getAttribute("isAuthenticated"))) {
            // 会话有效
            out.print("{\"valid\": true, \"username\": \"" + session.getAttribute("username") + "\"}");
        } else {
            // 会话无效
            out.print("{\"valid\": false}");
        }
        
        out.flush();
    }
}

